package com.android.kotlincoroutinemvvmdemo.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import com.android.kotlincoroutinemvvmdemo.R;
import com.android.kotlincoroutinemvvmdemo.util.LogX;

public class DynamicTextView extends View {
    private static final String TAG = "DynamicTextView";

    //默认文本
    private String text = "Hello, World!";
    //用于绘制文本的画笔
    private Paint textPaint;
    //文本的宽度
    private int textWidth;
    //文本的高度
    private int textHeight;

    public DynamicTextView(Context context) {
        super(context);
        init(context);
    }

    public DynamicTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DynamicTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //设置文本大小
        textPaint.setTextSize(context.getResources().getDimension(R.dimen.sp_15));
        //设置文本颜色
        textPaint.setColor(context.getResources().getColor(R.color.color_FF3D00));
        //计算文本的宽度和高度
        textWidth = (int) textPaint.measureText(text);
        textHeight = (int) (textPaint.descent() - textPaint.ascent());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int finalWidth;
        int finalHeight;

        //处理宽度
        if (widthMode == MeasureSpec.EXACTLY) {
            //使用父视图指定的宽度
            finalWidth = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            //不能超过父视图指定的最大宽度
            finalWidth = Math.min(textWidth, widthSize);
        } else {
            LogX.INSTANCE.d(TAG, "W-UNSPECIFIED");
            //UNSPECIFIED
            finalWidth = textWidth; //使用文本的宽度
        }

        //处理高度
        if (heightMode == MeasureSpec.EXACTLY) {
            //使用父视图指定的高度
            finalHeight = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            //不能超过父视图指定的最大高度
            finalHeight = Math.min(textHeight, heightSize);
        } else {
            LogX.INSTANCE.d(TAG, "H-UNSPECIFIED");
            //UNSPECIFIED
            finalHeight = textHeight; //使用文本的高度
        }

        //设置最终的测量结果
        setMeasuredDimension(finalWidth, finalHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制文本
        canvas.drawText(text, 0, textHeight - textPaint.descent(), textPaint);
    }

    //设置文本内容
    public void setText(String text) {
        this.text = text;
        //重新计算文本的宽度和高度
        textWidth = (int) textPaint.measureText(text);
        textHeight = (int) (textPaint.descent() - textPaint.ascent());
        //请求重新测量和绘制
        requestLayout();
        invalidate();
    }
}
